﻿using System.Windows;
using PDFDesign.Model.Const;
using PDFDesign.Command;
using System.Windows.Input;
using MahApps.Metro.Controls.Dialogs;
using PDFDesign.Model;
using PDFDesign.Repository;
using System.Collections.Generic;

namespace PDFDesign.ViewModel
{
    public class AddStyleViewModel : BaseViewModel
    {
        private string templateName;
        /// <summary>
        /// 模板名称
        /// </summary>
        public string TemplateName
        {
            get => templateName;
            set
            {
                templateName = value;
                OnPropertyChanged("TemplateName");
            }
        }

        private string styleName = "基本样式";
        /// <summary>
        /// 样式名称
        /// </summary>
        public string StyleName
        {
            get => styleName;
            set
            {
                styleName = value;
                OnPropertyChanged("StyleName");
            }
        }

        private float width;
        /// <summary>
        /// 宽
        /// </summary>
        public float Width
        {
            get => width;
            set
            {
                width = value;
                OnPropertyChanged("Width");
            }
        }

        private float height;
        /// <summary>
        /// 高
        /// </summary>
        public float Height
        {
            get => height;
            set
            {
                height = value;
                OnPropertyChanged("Height");
            }
        }

        private PDFSize pdfSize;
        /// <summary>
        /// PDF大小
        /// </summary>
        public PDFSize PDFSize
        {
            get => pdfSize;
            set
            {
                pdfSize = value;
                OnPropertyChanged("PDFSize");
            }
        }

        private bool enabledMode = true;
        /// <summary>
        /// 启用大小模式
        /// </summary>
        public bool EnabledMode
        {
            get => enabledMode;
            set
            {
                enabledMode = value;
                if (value)
                {
                    ModeVisibility = Visibility.Visible;
                    SizeVisibility = Visibility.Collapsed;
                }
                else
                {
                    ModeVisibility = Visibility.Collapsed;
                    SizeVisibility = Visibility.Visible;
                }
                OnPropertyChanged("EnabledMode");
            }
        }

        private Visibility modeVisibility;
        public Visibility ModeVisibility
        {
            get => modeVisibility;
            set
            {
                modeVisibility = value;
                OnPropertyChanged("ModeVisibility");
            }
        }

        private Visibility sizeVisibility = Visibility.Collapsed;
        public Visibility SizeVisibility
        {
            get => sizeVisibility;
            set
            {
                sizeVisibility = value;
                OnPropertyChanged("SizeVisibility");
            }
        }

        private bool isPortrait = true;
        /// <summary>
        /// 是否为纵向
        /// </summary>
        public bool IsPortrait
        {
            get => isPortrait;
            set
            {
                isPortrait = value;
                OnPropertyChanged("IsPortrait");
            }
        }

        private int fontSize = 10;
        /// <summary>
        /// 页码字体大小
        /// </summary>
        public int FontSize
        {
            get => fontSize;
            set
            {
                fontSize = value;
                OnPropertyChanged("FontSize");
            }
        }

        private int reportCount = 1;
        /// <summary>
        /// 每页报表数量
        /// </summary>
        public int ReportCount
        {
            get => reportCount;
            set
            {
                reportCount = value;
                OnPropertyChanged("ReportCount");
            }
        }

        private bool showPageNum;
        /// <summary>
        /// 是否显示页码
        /// </summary>
        public bool ShowPageNum
        {
            get => showPageNum;
            set
            {
                showPageNum = value;
                OnPropertyChanged("ShowPageNum");
            }
        }

        private float tableLeft = 50;
        /// <summary>
        /// 表格左边距
        /// </summary>
        public float TableLeft
        {
            get => tableLeft;
            set
            {
                tableLeft = value;
                OnPropertyChanged("TableLeft");
            }
        }

        private float tableTop = 80;
        /// <summary>
        /// 表格右边距
        /// </summary>
        public float TableTop
        {
            get => tableTop;
            set
            {
                tableTop = value;
                OnPropertyChanged("TableTop");
            }
        }

        private bool isFixedRowHeight;
        /// <summary>
        /// 是否固定行高
        /// </summary>
        public bool IsFixedRowHeight
        {
            get => isFixedRowHeight;
            set
            {
                isFixedRowHeight = value;
                OnPropertyChanged("IsFixedRowHeight");
            }
        }

        private float fixedRowHeight;
        /// <summary>
        /// 固定行高
        /// </summary>
        public float FixedRowHeight
        {
            get => fixedRowHeight;
            set
            {
                fixedRowHeight = value;
                OnPropertyChanged("FixedRowHeight");
            }
        }

        private bool isFixedRowCount;
        /// <summary>
        /// 是否固定行数
        /// </summary>
        public bool IsFixedRowCount
        {
            get => isFixedRowCount;
            set
            {
                isFixedRowCount = value;
                OnPropertyChanged("IsFixedRowCount");
            }
        }

        private int fixedRowCount;
        /// <summary>
        /// 固定行数
        /// </summary>
        public int FixedRowCount
        {
            get => fixedRowCount;
            set
            {
                fixedRowCount = value;
                OnPropertyChanged("FixedRowCount");
            }
        }

        private float tableHeight;
        /// <summary>
        /// 表格高度
        /// </summary>
        public float TableHeight
        {
            get => tableHeight;
            set
            {
                tableHeight = value;
                OnPropertyChanged("TableHeight");
            }
        }

        private bool renderWhiteRow;
        /// <summary>
        /// 是否渲染空白行
        /// </summary>
        public bool RenderWhiteRow
        {
            get => renderWhiteRow;
            set
            {
                renderWhiteRow = value;
                OnPropertyChanged("RenderWhiteRow");
            }
        }

        private string buttonText = "添加";
        public string ButtonText
        {
            get => buttonText;
            set
            {
                buttonText = value;
                OnPropertyChanged("ButtonText");
            }
        }

        /// <summary>
        /// 添加新样式
        /// </summary>
        public ICommand AddCommand { get => new AnotherCommand(_ => Add()); }
        private void Add()
        {
            if (string.IsNullOrWhiteSpace(TemplateName))
            {
                ShowMessage("请输入正确的模板名");
                return;
            }
            if (string.IsNullOrWhiteSpace(StyleName))
            {
                ShowMessage("请输入正确的样式名");
                return;
            }
            if (!EnabledMode && (Width <= 0 || Height <= 0))
            {
                ShowMessage("样式大小宽高应大于0");
                return;
            }
            if (FontSize < 5)
            {
                ShowMessage("字体过小会看不见,请调整字体字体大小");
                return;
            }
            if (ReportCount < 1)
            {
                ShowMessage("每页报表数量至少为1");
                return;
            }
            if (TableLeft < 0)
            {
                ShowMessage("请输入正确的表格左边距");
                return;
            }
            if (TableTop < 0)
            {
                ShowMessage("请输入正确的表格上边距");
                return;
            }
            if (IsFixedRowHeight && FixedRowHeight < 0)
            {
                ShowMessage("请输入正确得行高");
                return;
            }
            if (IsFixedRowCount && FixedRowCount < 0)
            {
                ShowMessage("请输入正确得行数");
                return;
            }
            if (!isModify && ReportStyleRepository.GetInstance().IsExistReportStyle(TemplateName, StyleName))
            {
                ShowMessage($"{TemplateName}的样式[{StyleName}]已存在");
                return;
            }
            var pageSetting = new PageSetting
            {
                FontSize = FontSize,
                Layout = IsPortrait ? Layout.portrait : Layout.landscape,
                ShowPageNum = ShowPageNum,
                ReportCount = ReportCount
            };
            if (EnabledMode)
            {
                pageSetting.SizeMode = PDFSize;
            }
            else
            {
                pageSetting.SizeArray = new float[] { Width, height };
            }
            var tableControl = new TableControl
            {
                FixedRowCount = FixedRowCount,
                IsFixedRowCount = IsFixedRowCount,
                FixedRowHeight = FixedRowHeight,
                IsFixedRowHeight = IsFixedRowHeight,
                Top = TableTop,
                Left = TableLeft,
                Height = TableHeight,
                RenderWhiteRow = RenderWhiteRow,
                Rows = new List<RowControl>()
            };
            if (isModify)
            {
                ReportStyleViewModel.ReportStyle.StyleName = StyleName;
                ReportStyleViewModel.ReportStyle.TemplateName = TemplateName;
                ReportStyleViewModel.PDFStyle.PageSetting = pageSetting;
                tableControl.Rows = ReportStyleViewModel.PDFStyle.Controls.Tables[0].Rows;
                ReportStyleViewModel.PDFStyle.Controls.Tables[0] = tableControl;
                DialogResult = true;
                return;
            }
            ReportStyleViewModel = new ReportStyleViewModel(TemplateName, StyleName, pageSetting, tableControl);
            DialogResult = true;
        }
        public ReportStyleViewModel ReportStyleViewModel { get; set; }

        private readonly bool isModify;

        public AddStyleViewModel()
        {
            isModify = false;
        }
        public AddStyleViewModel(ReportStyleViewModel viewModel)
        {
            isModify = true;
            ButtonText = "保存";
            TemplateName = viewModel.ReportStyle.TemplateName;
            StyleName = viewModel.ReportStyle.StyleName;
            ReportStyleViewModel = viewModel;
            FontSize = ReportStyleViewModel.PDFStyle.PageSetting.FontSize;
            IsPortrait = ReportStyleViewModel.PDFStyle.PageSetting.Layout == Layout.portrait;
            ShowPageNum = ReportStyleViewModel.PDFStyle.PageSetting.ShowPageNum;
            ReportCount = ReportStyleViewModel.PDFStyle.PageSetting.ReportCount;
            PDFSize = ReportStyleViewModel.PDFStyle.PageSetting.SizeMode;
            if (ReportStyleViewModel.PDFStyle.PageSetting.SizeArray == null || ReportStyleViewModel.PDFStyle.PageSetting.SizeArray.Length != 2)
            {
                EnabledMode = true;
            }
            else
            {
                Width = ReportStyleViewModel.PDFStyle.PageSetting.SizeArray[0];
                Height = ReportStyleViewModel.PDFStyle.PageSetting.SizeArray[1];
                EnabledMode = false;
            }
            FixedRowCount = ReportStyleViewModel.PDFStyle.Controls.Tables[0].FixedRowCount;
            IsFixedRowCount = ReportStyleViewModel.PDFStyle.Controls.Tables[0].IsFixedRowCount;
            FixedRowHeight = ReportStyleViewModel.PDFStyle.Controls.Tables[0].FixedRowHeight;
            IsFixedRowHeight = ReportStyleViewModel.PDFStyle.Controls.Tables[0].IsFixedRowHeight;
            TableTop = ReportStyleViewModel.PDFStyle.Controls.Tables[0].Top;
            TableLeft = ReportStyleViewModel.PDFStyle.Controls.Tables[0].Left;
            TableHeight = ReportStyleViewModel.PDFStyle.Controls.Tables[0].Height;
            RenderWhiteRow = ReportStyleViewModel.PDFStyle.Controls.Tables[0].RenderWhiteRow;
        }

        private bool? dialogResult;
        public bool? DialogResult
        {
            get => dialogResult;
            set
            {
                dialogResult = value;
                OnPropertyChanged("DialogResult");
            }
        }

        /// <summary>
        /// 提示消息
        /// </summary>
        /// <param name="message"></param>
        private void ShowMessage(string message) => DialogCoordinator.Instance.ShowMessageAsync(this, "温馨提示", message, MessageDialogStyle.Affirmative);
    }
}
